clear all
set printcolor gs1, permanently
set more off
global dirout ""
global dirgeolocalized ""
global diredomex ""

use "$diredomex/EDOMEX.dta", clear
split week_date, p("-")
destring week_date*, replace
drop week_date
rename  week_date1 year
rename  week_date2 month
rename  week_date3 day
gen all_dates=mdy(month, day, year)
drop month day year
format all_dates %td 
replace epaid_fares_usd=. if epaid_fares_usd==0
replace cash_fares_usd=. if cash_fares_usd==0
drop if epaid_fares_usd==. & cash_fares_usd==.
drop if epaid_fares_usd==0 & cash_fares_usd==0
drop if cash_trips==0 & epaid_trips==0
drop if cash_trips==. & epaid_trips==.

gen CARD=1 if epaid_fares_usd>0 & epaid_fares_usd!=.
gen CASH=1 if cash_fares_usd>0 & cash_fares_usd!=.

sort client all_dates
by client: gen n=_n 
by client: gen n_CARD=_n if CARD!=.
by client: gen n_CASH=_n if CASH!=.
by client: egen min_CARD=min(n_CARD)
by client: egen min_CASH=min(n_CASH)


gen type_CARD=1 if n==min_CARD
gen type_CASH=1 if n==min_CASH
replace type_CARD=0 if type_CARD==.
replace type_CASH=0 if type_CASH==.
gen type=type_CARD + type_CASH
bysort client_uuid: gen sum_type=sum(type)
tab sum_type

by client: egen sum_cash_fares_usd=sum(cash_fares_usd)
by client: egen sum_epaid_fares_usd=sum(epaid_fares_usd)
by client: egen sum_cash_trips=sum(cash_trips)
by client: egen sum_epaid_trips=sum(epaid_trips)
by client: egen min_hist_all_dates=min(all_dates)
by client: egen max_hist_all_dates=max(all_dates)

gen pure_cash=1 if sum_type==1 & CASH==1
gen pure_credit=1 if sum_type==1 & CARD==1
gen mixed=1 if sum_type==2
use "$diredomex/temp.dta", clear

use min_hist_all_dates client_uuid cash_trips epaid_trips cash_fares_usd epaid_fares_usd all_dates mixed using "$diredomex/temp.dta" if mixed==1, clear
gen tenure=(all_dates-min_hist_all_dates)/7 + 1
egen total_trips=rowtotal(cash_trips epaid_trips)
sort client all_dates
by client: gen hist_trips=sum(total_trips)
keep if hist_trips>5 & tenure>4
keep client_uuid
duplicates drop
save "$diredomex/clientid_mixedtrue.dta", replace


use trip_uuid client_uuid request_timestamp_local is_cash_trip original_fare_usd using "$dirgeolocalized/Datos Geolocalizados.dta", clear
merge m:1 client_uuid using "$diredomex/clientid_mixedtrue.dta", keep(match) nogenerate

gen cash=(is_cash_trip=="TRUE")
drop is_cash_trip

egen long trip=group(trip_uuid)
egen long user=group(client_uuid)
drop trip_uuid client_uuid
format trip user %15.0g

gen date_temp=substr(request,1,10)
split date, p("-")
drop if date_temp1=="2016"

destring date_temp1 date_temp2 date_temp3, replace
gen all_dates=mdy(date_temp2, date_temp3, date_temp1)
drop date* request
order all_dates user trip
gen ln_original_fare_usd=ln(original_fare_usd)
format all_dates %td

su original_fare_usd, d
drop if original_fare_usd<`r(p1)' 
su original_fare_usd, d

gen year=year(all_dates)
gen dow=dow(all_dates)

gen Monday=(dow==1)
gen Tuesday=(dow==2)
gen Wednesday=(dow==3)
gen Thursday=(dow==4)
gen Friday=(dow==5)
gen Saturday=(dow==6)
gen Sunday=(dow==0)

gen quincena=(all_dates==21042 | all_dates==21043 | all_dates==21044 | all_dates==21045 | all_dates==21046 | all_dates==21047 | all_dates==21048) | (all_dates==21056 | all_dates==21057 | all_dates==21058 | all_dates==21059 | all_dates==21060 | all_dates==21061) | (all_dates==21406 | all_dates==21407 | all_dates==21408 | all_dates==21409 | all_dates==21410 | all_dates==21411 | all_dates==21412) | (all_dates==21420 | all_dates==21421 | all_dates==21422 | all_dates==21423 | all_dates==21424 | all_dates==21425 | all_dates==21426)

gen quincenaxFriday=quincena*Friday
gen quincenaxSaturday=quincena*Saturday
gen quincenaxSunday=quincena*Sunday
gen quincenaxMonday=quincena*Monday
gen quincenaxTuesday=quincena*Tuesday
gen quincenaxWednesday=quincena*Wednesday
gen quincenaxThursday=quincena*Thursday
save "$diredomex/temp_truemixedusers.dta", replace




use "$diredomex/temp_truemixedusers.dta", clear
reghdfe cash Monday Tuesday Thursday Friday Saturday Sunday quincenaxFriday quincenaxSaturday quincenaxSunday quincenaxMonday quincenaxTuesday quincenaxWednesday quincenaxThursday ln_original_fare_usd, absorb(user year) cluster(user)
matrix results = [e(b)',vecdiag(e(V))']
		svmat results
		keep results*  
		drop if results1==.
		gen q=_n 
		drop if q<7
		drop if q>13

		rename results1 prob_cash
		cap drop c1 c2
		gen c1=prob_cash + 1.96*sqrt(results2)
		gen c2=prob_cash - 1.96*sqrt(results2)
		replace q=q-6
		
#delimit
twoway
(bar prob_cash q,  fcolor(ebg)  lcolor(black) )
(rcap c1 c2 q, lcolor(gs3) lw(vthin))
, ytitle("Probability of paying with cash (after payment)") title(" ") ylabel(-0.01[0.01]0.02) 
xlabel(1 "F" 2 "S" 3 "Su" 4 "M" 5 "T" 6 "W" 7 "Th")
xtitle("") 
legend(off)
graphregion(color(white)) plotregion(fcolor(white));
#delimit cr	
graph export "$dirout/Figure12.eps", replace



		
		
		
		 
